<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item prop="categoryId" label="档案类型">
          <el-select v-model="dataForm.categoryId" placeholder="档案类型">
            <el-option v-for="category in classList" :key="category.id" :label="category.name" :value="category.id">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item prop="transferTitle" label="移交标题">
            <el-input v-model="dataForm.transferTitle" placeholder="请输入移交标题"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="transferTenantCode" label="接收单位">
            <el-select v-model="dataForm.transferTenantCode" placeholder="请选择接收单位">
              <el-option v-for="tenant in tenantList" :key="tenant.id" :label="tenant.tenantName" :value="tenant.tenantCode"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="recipient" label="下一步接收人">
            <el-input v-model="dataForm.recipient" placeholder="请输入下一步接收人"></el-input>
          </el-form-item>

        </el-col>
      </el-row>
      <el-form-item prop="remark" label="移交说明">
        <el-input type="textarea" :rows="4" placeholder="请输入移交说明" v-model="dataForm.remark"> </el-input>
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <p>档案列表</p>
          <el-table  style="width: 100%; margin-bottom: 10px">
            <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
            <el-table-column label="序号" prop="code" sortable="custom" header-align="center" align="center"></el-table-column>
            <el-table-column label="档号" prop="fileNumber" header-align="center" align="center"></el-table-column>
            <el-table-column label="题名" prop="title" header-align="center" align="center"></el-table-column>
            <el-table-column label="档号类型" prop="categoryName" header-align="center" align="center"></el-table-column>
            <el-table-column label="密级" prop="createDate" header-align="center" align="center" width="180"></el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
  import { defineComponent, reactive } from "vue";
  import baseService from "@/service/baseService";
  import { useDebounce } from "@/utils/utils";
  import { IObject } from "@/types/interface";
  export default defineComponent({
    setup() {
      return reactive({
        visible: false,
        classList: [] as IObject[],
        tenantList: [] as IObject[],
        dataForm: {
          id: "",
          fileNumberList:[] as IObject[],
          categoryId: "",
          transferTitle: "",
          transferTenantCode: "",
          recipient:"",
          remark:""
        }
      });
    },
    computed: {
      dataRule() {
        return {
          type: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }]
        };
      }
    },
    created() {
      this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
    },
    methods: {
      init() {
        this.visible = true;
        this.$nextTick(() => {
          this.$refs["dataFormRef"].resetFields();
          this.getClassList()
          this.getOrgInfo();
        });
      },
      getOrgInfo() {
        baseService.get(`/sys/tenant/listAll`).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.tenantList = res.data;
        });
      },
      getClassList() {
        return baseService.get("/archive/class/list/no-tree").then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.classList = res.data;
        });
      },
      dataFormSubmitHandle() {
        this.$refs["dataFormRef"].validate((valid: boolean) => {
          if (!valid) {
            return false;
          }
          (!this.dataForm.id ? baseService.post : baseService.put)("/archive/transfer-list", {
            ...this.dataForm
          }).then((res) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              message: this.$t("prompt.success"),
              type: "success",
              duration: 500,
              onClose: () => {
                this.visible = false;
                this.$emit("refreshDataList");
              }
            });
          });
        });
      },
    }
  });
</script>

<style lang="less" scoped>
  .mod-tenant__tenant {
    .role-list {
      .el-select {
        width: 100%;
      }
    }
  }
  /deep/ .el-input__inner {
    width: 230px;
  }
</style>
